package com.bianguo.android.functiondemo.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Collection;

import butterknife.ButterKnife;

public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<BaseRecyclerAdapter.BaseViewHolder> {

    protected Context mContext;
    protected ArrayList<T> list;
    protected LayoutInflater inflater;
    protected int headCount, footCount;
    protected OnClickItemListener onClickItemListener;

    public ArrayList<T> getList() {
        return list;
    }

    public void setOnClickItemListener(OnClickItemListener onClickItemListener) {
        this.onClickItemListener = onClickItemListener;
    }

    public BaseRecyclerAdapter(Context mContext, ArrayList<T> list) {
        this.mContext = mContext;
        this.list = list;
        inflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return createHolder(viewGroup, i);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder baseViewHolder, int i) {
        bindHolder(baseViewHolder, i);
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() + headCount + footCount : headCount + footCount;
    }

//    public void setOnClickViewListener(final BaseRecyclerAdapter adapter, View view, final int position) {
//        view.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (onClickItemListener != null) {
//                    onClickItemListener.onClickItemListener(adapter, view, position);
//                }
//            }
//        });
//    }

    public void addAll(Collection<? extends T> collections) {
        if (collections != null) {
            list.addAll(collections);
            notifyDataSetChanged();
        }
    }

    public abstract BaseViewHolder createHolder(@NonNull ViewGroup viewGroup, int viewType);

    public abstract void bindHolder(@NonNull BaseViewHolder baseViewHolder, int position);

    public static class BaseViewHolder extends RecyclerView.ViewHolder {

        public BaseViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnClickItemListener {
        void onClickItemListener(BaseRecyclerAdapter adapter, View view, int position);
    }
}
